﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using fsw.Common;
using System.Text;

namespace fsw.Web.SytemData
{
    public partial class ListPid : Web.UI.ManagePage
    {
        protected Model.Admin model_admin;
        protected int _Pid = 0;//项目id
        protected int totalCount;
        protected int page;
        protected int pageSize;
        protected string keywords = string.Empty;
        protected string PageContent = string.Empty;
        protected string Path_File = "/" + DTKeys.Path_File_No;
        protected DataTable rptList;
        protected List<Model.Project> List_Project = new List<Model.Project>();//
        protected string List_Pro_Str = string.Empty;
        protected string List_User_Str = string.Empty;
        protected string List_User_Type = string.Empty;
        protected int _UserId = 0;
        protected int _UserType = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            model_admin = GetAdminInfo();
            if (model_admin.TypeId > 1) Response.End();
            this.keywords = DTRequest.GetQueryString("keywords");
            this.pageSize = Utils.GetPageSize(24, "SytemData_ListPid_page_size");
            this.page = DTRequest.GetQueryInt("page", 1);
            this._Pid = DTRequest.GetQueryInt("pid");
            this._UserId = DTRequest.GetQueryInt("userid");
            this._UserType = DTRequest.GetQueryInt("usertype");
            if (!Page.IsPostBack)
            {
                RptBind("Id > 0 and isStatus = 1" + CombSqlTxt(), "PicNum desc");
                RptProject(); RptUser();
            }
        }
        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="_strWhere"></param>
        /// <param name="_orderby"></param>
        private void RptBind(string _strWhere, string _orderby)
        {
            BLL.Admin_Pic bll = new BLL.Admin_Pic();
            rptList = bll.GetListStr(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount).Tables[0];
            string pageUrl = Utils.CombUrlTxt("ListPid.aspx", "keywords={0}&pid={1}&userid={2}&usertype={3}&page={4}",
                this.keywords, this._Pid.ToString(), this._UserId.ToString(), this._UserType.ToString(), "__id__");
            this.PageContent = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void RptProject()
        {
            List_Project = new BLL.Project().GetList();
            StringBuilder Sb = new StringBuilder();
            Sb.Append("<option value=\"\">请选择项目</option>");
            foreach (var m in List_Project)
            {
                string _selected = m.Pid == this._Pid ? "selected=\"selected\"" : "";
                Sb.AppendFormat("<option value=\"{0}\"{2}>{1}</option>", m.Pid.ToString(), m.HosName, _selected);
            }
            List_Pro_Str = Sb.ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private string CombSqlTxt()
        {
            StringBuilder strTemp = new StringBuilder();
            if (this._Pid > 0) strTemp.Append(" and Pid=" + this._Pid.ToString());
            if (this._UserId > 0) strTemp.Append(" and UserId=" + this._UserId.ToString());
            if (this._UserType > 0)
            {
                strTemp.Append(this._UserType == 1 ? " and EXISTS(select top 1 Id from Y_Admin b where PatIndex('%[^0-9]%', b.UserName) = 0 and UserId = b.Id)" : " and EXISTS(select top 1 Id from Y_Admin b where PatIndex('%[^0-9]%', b.UserName) > 0 and UserId = b.Id)");
            }
            if (!string.IsNullOrEmpty(this.keywords)) strTemp.Append(" and (PicPath like '%" + this.keywords + "%' or PicName like '%" + this.keywords + "%')");
            return strTemp.ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        private void RptUser()
        {
            List<Model.Admin> _list = new List<Model.Admin>();
            StringBuilder strTemp = new StringBuilder();
            BLL.Admin bll = new BLL.Admin();
            _list = bll.GetList(100, " exists(select UserId from Y_Admin_Pic where UserId = a.Id) ", " a.UserName asc");
            strTemp.Append("<option value=\"\">请选择用户</option>");
            foreach (var m in _list)
            {
                string _selected = m.Id == this._UserId ? "selected=\"selected\"" : "";
                strTemp.AppendFormat("<option value=\"{0}\"{2}>{1}</option>", m.Id, m.UserName + "(" + m.RealName + ")", _selected);
            }
            List_User_Str = strTemp.ToString();
            StringBuilder strList = new StringBuilder();
            strList.Append("<option value=\"0\">请选择  </option>");
            strList.AppendFormat("<option value=\"{0}\"{2}>{1}</option>", 1, "咨询", this._UserType == 1 ? "selected=\"selected\"" : "");
            strList.AppendFormat("<option value=\"{0}\"{2}>{1}</option>", 2, "非咨询", this._UserType == 2 ? "selected=\"selected\"" : "");
            List_User_Type = strList.ToString();
        }
    }
}